﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.manager
{
    public partial class operatoradd : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet roles = PhoneBook.Manage.SysRole.getManagerRole();
            Commons.ControlUtil.BindDDL(ddl_roleid, roles, "rolename", "roleid");
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            if (PhoneBook.Users.Users.isExists(txb_username.Text.Trim()))
            {
                Helper.ShowMessage(u_message, controls.UCMessageType.attention, "用户名已存在");
                return;
            }

            if (!txb_userpwd.Text.Equals(txb_userpwd2.Text))
            {
                Helper.ShowMessage(u_message, controls.UCMessageType.attention, "两次的密码不一致");
                return;
            }

            Hashtable newuser = new Hashtable();
            newuser["username"] = txb_username.Text;
            Dx.ZcEncrypt.Encrypt3DES encrypt = new Dx.ZcEncrypt.Encrypt3DES();
            newuser["userpwd"] = txb_userpwd.Text;//encrypt.EncryptString(Dx.ZcEncrypt.Encrypt.GetMD5(txb_userpwd.Text));
            newuser["nickname"] = txb_nickname.Text;
            if (!string.IsNullOrEmpty(Request["groupid"]))
            {
                newuser["groupid"] = Request["groupid"];
                PhoneBook.Users.Users.addgroupmanager(newuser);
            }
            else
                PhoneBook.Users.Users.addmanageruser(newuser);

            if (!string.IsNullOrWhiteSpace(ddl_roleid.SelectedValue))
            {
                string userid = PhoneBook.Users.Users.getByName(newuser["username"].ToString())["userid"].ToString();
                string roleid = ddl_roleid.SelectedValue;
                PhoneBook.Manage.SysUserRole.add(userid, roleid);
            }
            AddLog("新添加了管理员：{0};", newuser["username"]);
            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
        }
    }
}